<?php
class ManageController extends Yaf_Controller_Abstract{
    public function indexAction(){
        $isGet = $this->_request->isGet();
        if($isGet){
            return true;
        }else{
            $verify = Request::aspost(6,'verifycode',null);
            $name = Request::aspost(6,'user_name',null);
            $password = Request::aspost(6,'user_password',null);
            if($verify == null || $name == null || $password == null){
                $warning = '请填上用户名，密码，和验证码';
                $this->_view->warning= $warning;
            }else{
                $verifyObject = new Verify_Code;
                $flag = $verifyObject->check($verify,'2');
                if($flag){
                    $userData = Db::table('user')->where(['user_name' => $name])->one();
                    $model= new UserModel;
                    $rules = $model->getRule($userData['user_id']);
                    if(empty($userData)){
                        $this->_view->warning = 'The User Name Is Wrong';
                    }else{
                        $flag = password_verify($password,$userData['user_password']);
                        if(true == $flag){
                            $user = [
                                'id' =>  $userData['user_id'],
                                'name' =>  $userData['user_name'],
                                'role' =>  $userData['role_id'],
                                'ip' => Request::ip(),
                            ];
                            $session = Yaf_Session::getInstance();
                            $session->set('manager', $user);
                            $session->set('rules',$rules);
                            $this->redirect('/admin/index/index');                        
                        }else{
                            $this->_view->warning =  ' Password Code Is Wrong';
                        }
                    }
                }else{
                    $this->_view->warning = 'The Verify Code Is Wrong';
                }
            }
        }
    }
    public function loginAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $isGet = $this->_request->isGet();
        if($isGet){
            echo "System Error! Oh,I will die.........";
            exit;
        }else{
            $verify = Request::aspost(6,'verifycode',null);
            $name = Request::aspost(6,'user_name',null);
            $password = Request::aspost(6,'user_password',null);
            if($verify == null || $name == null || $password == null){
                $data = [
                    'warning' =>'请填上用户名，密码，和验证码',
                ];
                $this->redirect('index');
                return false;
            }else{
                $verifyObject = new Verify_Code;
                $flag = $verifyObject->check($verify,'2');
                var_dump($flag);
                if($flag){
                    $userData = Db::table('user')->where(['user_name' => $name])->one();
                    if(empty($userData)){
                        $data = [
                            'warning' =>'The User Name Is Wrong',
                        ];
                        $this->redirect('index');                    
                        return false;
                    }else{
                        $flag = password_verify($password,$userData['user_password']);
                        if(true == $flag){
                            $user = [
                                'id' =>  $userData['user_id'],
                                'name' =>  $userData['user_name'],
                                'role' =>  $userData['role_id'],
                                'ip' => Request::ip(),
                            ];
                            $session = Yaf_Session::getInstance();
                            $session->set('user', $user);
                            $this->redirect('/admin/index/index');                        
                        }else{
                            $data = [
                                'warning' =>'The Password Code Is Wrong',
                            ];
                            $this->display('index',$data);                    
                            return false;                        
                        }
                    }
                }else{
                    $data = [
                        'warning' =>'The Verify Code Is Wrong',
                    ];
                    $this->display('index',$data);                    
                    return false;                
                }
            }
        }
    }
    public function logoutAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $session = Yaf_Session::getInstance();
        $session->del('manager');
        $this->redirect('/admin/manage/index');
    }
    public function errorAction(){}
    public function editAction(){
        $id = Request::filter('id',0,4,null);
        
    }
    public function verifyAction(){
        $verify = new Verify_Code;
        $verify->execute('2');
    }
    public function mAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        
        $d = Yaf_Session::getInstance();
        $d->start();
        $data = [1,2,3,4,5];
        $d->set('jjj',$data);
    }
    public function lAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        // var_dump($this);
        // $verify = Request::aspost(6,'verifycode',null);
        // $verifyObject = new Verify_Code;
        // $flag = $verifyObject->check($verify,'2');
        // echo $flag;
        // $session = Yaf_Session::getInstance();
        $verify = Request::aspost(6,'verifycode',null);
        echo $verify;
        $verifyObject = new Verify_Code;
        $flag = $verifyObject->check($verify,'2');
        var_dump($flag);
        // $session = Yaf_Registry::get('session');
        // $session->get('jjj');
    }
}
?>
